package com.taylor.seckill.controller;

import com.taylor.seckill.pojo.User;
import com.taylor.seckill.redis.GoodsKey;
import com.taylor.seckill.service.IGoodsService;
import com.taylor.seckill.service.IUserService;
import com.taylor.seckill.util.RedisUtil;
import com.taylor.seckill.vo.GoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.IWebContext;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author taylor
 * @Description: TODO
 * @date 2021/4/9 23:00
 */
@Controller
@RequestMapping("/goods")
@Slf4j
public class GoodsController {


    @Autowired
    private IUserService userService;

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    ThymeleafViewResolver thymeleafViewResolver;

    @Autowired
    RedisUtil redisUtil;

    @RequestMapping(value = "/to_List",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toList(Model model, User user, HttpServletRequest request, HttpServletResponse response){
        model.addAttribute("user",user);
        //取缓存
        String html = redisUtil.get(GoodsKey.getGoodsList,"",String.class);
        if (!StringUtils.isEmpty(html)){
            return html;
        }

        //查询商品列表
        List<GoodsVo> goodsList = goodsService.findGoodsVo();
        model.addAttribute("goodsList",goodsList);
//		return "goods_list";

        IWebContext context = new WebContext(request,response,
                request.getServletContext(),
                request.getLocale(),
                model.asMap()
        );
        //手动渲染
        html = thymeleafViewResolver.getTemplateEngine().process("goodsList",context);

        if (!StringUtils.isEmpty(html)) {
            redisUtil.set(GoodsKey.getGoodsList, "", html);
        }
        return html;
    }
    @RequestMapping("/toList")
    public String goodsList(Model model, User user){

//        log.info("ticket============"+ticket);
//        if (StringUtils.isEmpty(ticket)){
//            return "login";
//        }
//        User user = userService.getUserByCookie(ticket, request, response);
        log.info("user :"+user);
        if (null == user){
            return "login";
        }
        model.addAttribute("user",user);
        model.addAttribute("goodsList",goodsService.findGoodsVo());
        return "goodsList";
    }

    @RequestMapping("/toGoodsDetail/{id}")
    public String goodsDetail( Model model, User user,
                              @PathVariable("id")long id){

        log.info("user :"+user);
        if (null == user){
            return "login";
        }

        int secKillStatus = 0;
        //秒杀倒计时
        int remainSeconds = 0;

        model.addAttribute("user",user);
        GoodsVo goodsVo = goodsService.findGoodsVoByID(id);
        Date startDate = goodsVo.getStartDate();
        Date endDate = goodsVo.getEndDate();
        Date nowDate = new Date();

        if (nowDate.before(startDate)){
            remainSeconds = (int) ((startDate.getTime() - nowDate.getTime()) / 1000);
        }else if (nowDate.after(endDate)){
            secKillStatus = 2;
            remainSeconds= -1;
        }else {
            secKillStatus = 1;
            remainSeconds = 0;
        }


        model.addAttribute("remainSeconds",remainSeconds);
        model.addAttribute("secKillStatus",secKillStatus);
        model.addAttribute("goods",goodsService.findGoodsVoByID(id));
        return "goodsDetail";
    }
}
